/**
  ***********************************************************************************************************************
  * File Name          : UART_Printf.h
  * Description        : This file contains all the functions prototypes for
  *                      Connect to ESP8266 by USART1
  *
  * This file includes: ESP8266 initialization via RST pin, ESP8266 basic AT instruction send and receive function, ESP8266 AT-TCP 
  * instruction send and receive function, ESP8266 AT-MQTT instruction send and receive function
  *
  * Note that the void USART1_IRQHandler() function in stm32f1xx_it.c has been modified to receive ESP8266 serial port data frames
  ***********************************************************************************************************************
  */
  
/* Define to prevent recursive inclusion --------------------------------------------------------------------------*/
#ifndef __CONNECT_H
#define __CONNECT_H
#ifdef __cplusplus
 extern "C" {
#endif

/* Includes -------------------------------------------------------------------------------------------------------*/
#include "main.h"
/* Common macro definitions----------------------------------------------------------------------------------------*/

/* Anonymous structure-related compiler options 
   Related to ESP8266_Fram_Record_Struct after
*/
#if defined ( __CC_ARM   )
#pragma anon_unions
#endif

/* Serial port 1 macro definitions */
#define MAX_REC_LENGTH         			 1024 

/* Device-specific macro definitions-------------------------------------------------------------------------------*/

/* Pin macro definition */
/* Pins about the reset function*/
#define ESP8266_RST_Pin 			GPIO_PIN_12
#define ESP8266_RST_GPIO_Port 		GPIOB
/* Pins about enabling functions */
#define ESP8266_CH_PD_Pin			GPIO_PIN_13
#define ESP8266_CH_PD_GPIO_Port 	GPIOB
/* ESP8288 Sending and receiving port */
#define ESP8266_TX_Pin 				GPIO_PIN_9
#define ESP8266_TX_GPIO_Port 		GPIOA
#define ESP8266_RX_Pin 				GPIO_PIN_10
#define ESP8266_RX_GPIO_Port 		GPIOA

/* Macro defined function-------------------------------------------------------------------------------------------*/

/* A macro function that calculates the length of an array */
#define COUNTOF(__BUFFER__)   		(sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))

#define ESP8266_RST_Pin_SetH       	(HAL_GPIO_WritePin(ESP8266_RST_GPIO_Port, ESP8266_RST_Pin, GPIO_PIN_SET))
#define ESP8266_RST_Pin_SetL       	(HAL_GPIO_WritePin(ESP8266_RST_GPIO_Port, ESP8266_RST_Pin, GPIO_PIN_RESET))

#define ESP8266_CH_PD_Pin_SetH    	HAL_GPIO_WritePin(ESP8266_CH_PD_GPIO_Port, ESP8266_CH_PD_Pin, GPIO_PIN_SET)
#define ESP8266_CH_PD_Pin_SetL     	HAL_GPIO_WritePin(ESP8266_CH_PD_GPIO_Port, ESP8266_CH_PD_Pin, GPIO_PIN_RESET)

/* The macro implementation of C language in the default parameter*/
#define ESP8266_USART(fmt, ...)    	USART1_printf (fmt, ##__VA_ARGS__) 

/* Extern Variable-------------------------------------------------------------------------------------------------*/
/* Serial port receiving area, data cache */
extern uint8_t RxBuffer; 

/* Data structure declaration--------------------------------------------------------------------------------------*/
/* Enumeration of esp8266 working modes */
typedef enum
{
    STA,
    AP,
    STA_AP  
}ENUM_Net_ModeTypeDef;

/* Enumeration of network transport layer protocols */
typedef enum{
     enumTCP,
     enumUDP,
} ENUM_NetPro_TypeDef;

/* Enumeration of connection numbers to prevent other computers from accessing the same port */
typedef enum{
    Multiple_ID_0 = 0,
    Multiple_ID_1 = 1,
    Multiple_ID_2 = 2,
    Multiple_ID_3 = 3,
    Multiple_ID_4 = 4,
    Single_ID_0 = 5,
} ENUM_ID_NO_TypeDef;

/* Data frame structure definition */
/* !!! An error occurs if you do not add the extern keyword */
extern struct STRUCT_USART_Fram  
{
    char Data_RX_BUF[MAX_REC_LENGTH];
    union 
    {
        __IO uint16_t InfAll;
		/* If you want MDK5 to support anonymous structures, you need to compile the #pragma anon_unions directive */
        struct 
        {
            __IO uint16_t FramLength       :15;                               // 14:0 
            __IO uint16_t FramFinishFlag   :1;                                // 15 
        }InfBit;
    }; 
	
}ESP8266_Fram_Record_Struct;

/* Function declaration--------------------------------------------------------------------------------------------*/

/* Initialization and TCP function functions */

/* ESP8266 restart function */
uint8_t ESP8266_Rst(void);
/* ESP8266 initialization */
uint8_t ESP8266_Init(void);
/* Set the time domain and SNTP server */
uint8_t ESP8266_Set_SNTP(uint32_t timezone , char * server_0 , char * server_1 , char * server_2);
/* Send the AT command to the ESP8266 module */
uint8_t ESP8266_Send_AT_Cmd(char *cmd,char *ack1,char *ack2,uint32_t time);
/* Send AT command to restore factory Settings and erase the saved parameters */
uint8_t ESP8266_AT_RESTORE(void);
/* Select the ESP8266 working mode */
uint8_t ESP8266_Net_Mode_Choose(ENUM_Net_ModeTypeDef enumMode);
/* Connect to external WIFI  */
uint8_t ESP8266_JoinAP( char * pSSID, char * pPassWord);
/* Whether to use multi-link mode in transparent transport  */
uint8_t ESP8266_Enable_MultipleId (FunctionalState enumEnUnvarnishTx );
/* The ESP8266 connects to the server over TCP or UDP */
uint8_t ESP8266_Link_Server(ENUM_NetPro_TypeDef enumE, char * ip, char * ComNum, ENUM_ID_NO_TypeDef id);
/* The transparent transmission function was enabled */
uint8_t ESP8266_UnvarnishSend (void);
/* Controls ESP8266 to send strings */
uint8_t ESP8266_SendString(FunctionalState enumEnUnvarnishTx, char * pStr, uint32_t ulStrLength, ENUM_ID_NO_TypeDef ucId );
/* ESP8266 exits transparent transmission mode */
uint8_t ESP8266_ExitUnvarnishSend ( void );
/* The ESP8266 detects the network connection status */
uint8_t ESP8266_Get_LinkStatus ( void );

/* Functions related to the MQTT instruction */

/* MQTT configures user attributes */
uint8_t ESP8266_MQTTUSERCFG( char * pClient_Id, char * pUserName,char * PassWord);
/* Set the MQTT client ID : Generated by aliyun Iot platform configuration tool */
uint8_t ESP8266_MQTTSETCLIENTID( char * p_Generate_Client_Id );
/* Connect to the specified MQTT server */
uint8_t ESP8266_MQTTCONN( char * Ip, uint32_t Num);
/* Subscribe to MQTT topics that are specified for connection */
uint8_t ESP8266_MQTTSUB(char * Topic);
/* Publish data on LinkID via topic, where data is a string message */
uint8_t ESP8266_MQTTPUB( char * Topic,char *temp);
/* Close the connection of the MQTT Client to LinkID and release the internal resources */
uint8_t ESP8266_MQTTCLEAN(void);
/* ESP8266 Sends a character string */
uint8_t MQTT_SendString(char * pTopic,char *temp);
/* ESP8266 detects the MQTT connection status */
uint8_t ESP8266_Get_MQTTStatus ( void );
/* The MQTT sends heartbeat packets to maintain the connection */
uint8_t ESP8266_Send_Heart( void );

#ifdef __cplusplus
}
#endif
#endif /* __CONNECT_H */


